﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SurvivalMan
{
    /// <summary>
    /// This class creates the player
    /// </summary>
    class Player
    {
        private int Xposition;
        private int Yposition;
        private int direction = 0;
        public int points = 0, amm = 1, prevpoints = 0;
        public Vector2 position, arrowPos;
        public Texture2D playerLeft, playerRight;
        private SpriteFont poäng, ammo, tid, start;
        private bool jumping;
        private int jumpspeed = 0;
        private int ground = 390;
        private Arrow a1;
        private string tiden1;
        public float tiden, prevtime;
        public int gameState = 0;


        public Color[] playerTextureData;
        /// <summary>
        /// To create a player this parameters below are needed.
        /// Parameters explained:
        /// Xpos and Ypos are the starting position for the player.
        /// left and right are two texture2D of the player picture, one for each direction the player are looking at.
        /// a1 is another class needed for the shooting.
        /// poäng, ammo and tid are 3 spritefonts needed for the output on screen witch shows points, ammo and ingame time.
        /// </summary>
        /// <param name="Xpos"></param>
        /// <param name="Ypos"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="a1"></param>
        /// <param name="poäng"></param>
        /// <param name="ammo"></param>
        /// <param name="tid"></param>

        public Player(int Xpos, int Ypos, Texture2D left, Texture2D right, Arrow a1, SpriteFont poäng, SpriteFont ammo, SpriteFont tid, SpriteFont start)
        {
            this.Xposition = Xpos;
            this.Yposition = Ypos;
            this.playerLeft = left;
            this.playerRight = right;
            this.a1 = a1;
            this.poäng = poäng;
            this.ammo = ammo;
            this.tid = tid;
            this.start = start;
        }

        public int getX() { return Xposition; }
        public int getY() { return Yposition; }
        public int getP() { return points; }
        public int getGS() { return gameState; }

        /// <summary>
        /// This function draws everything that has to do with the player, the player, the points, his ammo and some more stuff
        /// </summary>
        /// <param name="spritebatch"></param>
        public void draw(SpriteBatch spritebatch)
        {
            position = new Vector2(Xposition, Yposition);
            if (gameState == 1)
            {
                if (direction == 0)
                {
                    spritebatch.Draw(playerLeft, position, Color.White);
                    a1.draw(spritebatch);
                }
                else if (direction == 1)
                {
                    spritebatch.Draw(playerRight, position, Color.White);
                    a1.draw(spritebatch);
                }
                spritebatch.DrawString(poäng, points.ToString(), new Vector2(85, 445), Color.White);
                spritebatch.DrawString(ammo, amm.ToString() + "/1", new Vector2(700, 445), Color.Blue);
                spritebatch.DrawString(tid, tiden1, new Vector2(360, 445), Color.White);
            }
            else if (gameState == 0)
            {       
                spritebatch.DrawString(start, "PRESS ENTER TO PLAY \nYOUR POINTS FROM THE PREVIOUS GAME :" + prevpoints.ToString(), new Vector2(200, 200), Color.White);
            }
        }
        /// <summary>
        /// This function handle all the movment from the player such as moving sideways or jumping, it also handles the ingame time.
        /// The function also checks if the player shots an arrow, if so the draw function draws it in the screen.
        /// </summary>
        /// <param name="gameTime"></param>
        public void move(GameTime gameTime)
        {
            arrowPos.X = Xposition;
            arrowPos.Y = Yposition;
            MouseState mus = Mouse.GetState();
            KeyboardState tagentbord = Keyboard.GetState();

            Xposition = mus.X; // låter spelarens X-värde ändras med musen

            tiden += (float)gameTime.ElapsedGameTime.TotalSeconds;
            tiden1 = string.Format("{0:0}", tiden);

            if(mus.LeftButton == ButtonState.Pressed && amm == 1)
            {
                direction = 0;
                a1.hastighet.X = -15;
                a1.position = arrowPos;
                amm = 0;
            }            

            if (mus.RightButton == ButtonState.Pressed && amm ==1)
            {
                direction = 1;
                a1.hastighet.X = 15;
                a1.position = arrowPos;
                amm = 0;
            }
            
                a1.position += a1.hastighet;

            if (a1.position.X < 0)
                amm = 1;
            else if (a1.position.X > 800)
                amm = 1;
            if (tagentbord.IsKeyDown(Keys.Enter))
            {
                gameState = 1;
                tiden = 0;
            }

            if (jumping) //tillåter spelaren att hoppa
            {
                Yposition += jumpspeed;
                jumpspeed += 1;
                if (Yposition >= ground)
                {
                    Yposition = ground;
                    jumping = false;
                }
            }
            else
            {
                if (tagentbord.IsKeyDown(Keys.Space))
                {
                    jumping = true;
                    jumpspeed = -13;
                }
            }

            // kollar så att spelare inte lämnar skärmen
            if (Xposition > 800)
                Xposition = 750;
            else if (Xposition < 0)
                Xposition = 50;

        }

    }
}
